package yl.hs.bmipfull.service.laboratory.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratory.IProductCateDao;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.pojo.laboratory.ProductCateEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialCateEntity;
import yl.hs.bmipfull.service.laboratory.IProductCateService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  产品类别表服务层接口实现<br/>
 * 创建时间: 2021年05月24日 10时11分57秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ProductCateServiceImpl extends LaboratoryBaseService implements IProductCateService {

    @Autowired
    IProductCateDao dao;
    @Autowired
    IProductInfoDao productInfoDao;

    @Override
    public List<ProductCateEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductCateEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public PageResult<ProductCateEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductCateEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);

        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        PageResult<ProductCateEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), ProductCateEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(), filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(), filters));
        return result;
    }


    @Override
    public ProductCateEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveProductCate(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        if (Tl.isEmpty(sn)) {
            //执行保存
            ProductCateEntity entity = new ProductCateEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProductCateEntity.class));
            entity.setOrganizationCode(getDepartSN());
            var catelist = dao.chackByName(entity.getCateName(),null);
            if (catelist != null) {
                result = -1;
            } else {
                result = dao.insertNew(entity);
                if (result > 0) {
                    writeLog(ActionType.CREATE, entity.getSn(), "插入产品类别表信息", Tl.toJson(entity), ProductCateEntity.class);
                }
            }

        } else {
            //执行修改
            var entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setOrganizationCode(getDepartSN());
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, ProductCateEntity.class);
            }
        }

        return result;
    }

    @Override
    public Integer delete(String[] sn) {
        List<String> newSN=new ArrayList<>();
        var info=productInfoDao.queryByCateSn(sn[0]);
        if(info.size()>0){
            return -1;
        }
        for (var str:sn) {
            var list=productInfoDao.queryByCateSn(str);
            if(list.size()==0){
                newSN.add(str);
            }
        }
        var result = dao.changeStateBySn(newSN.toArray(new String[newSN.size()]), DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, ProductCateEntity.class);
        }
        return result;
    }

    @Override
    public List<ProductCateEntity> findList() {
        //var cmpySNList = getCompanySNList();
        var cmpySNList=new ArrayList<String>();
        return dao.findList(cmpySNList);
    }

    @Override
    public Map<String, Object> getAllCateMap() {
        var obj = new HashMap<String, Object>();
        var cateList = dao.findList(getCompanySNList());
        if (cateList.size() > 0) {
            for (int i = 0; i < cateList.size(); i++) {
                var cate = cateList.get(i);
                if (!obj.containsKey(cate.getSn())) {
                    obj.put(cate.getSn(), cate.getCateName());
                }
            }
        }
        return obj;
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result = dao.changeStateBySn(snArray, state);
        if (result > 0) {
            forWriteLog(snArray, state);
        }
        return result;
    }

    @Override
    public Boolean queryByName(String cateName, String sn) {
        var  Entity = dao.chackByName(cateName,sn);
        if (Entity == null){
            return true;
        }else {
            return false;
        }
    }
}